
package magichands.core.model.accessibility

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import magichands.core.OpenApi;


public class multi {
    private GestureDescription.StrokeDescription mStrokeDescription = null;
    private final AtomicBoolean runTouch = new AtomicBoolean(false);
    private List<GestureDescription> bsp = new LinkedList();
    private boolean m = false;
    private int x = 0;
    private int y = 0;



    public boolean touchDown(int i, int i2, int i3) {
        return xtouchDown(i, i2, i3);
    }


    public boolean touchMove(int i, int i2, int i3) {
        return xtouchMove(i, i2, i3);
    }


    public boolean touchUp(int i, int i2, int i3) {
        return xtouchUp(i, i2, i3);
    }
    public boolean xtouchDown(int x, int y, long j) {
        boolean is;
        synchronized (runTouch) {
            this.bsp.add(createGestureDescription(x, y, x, y, 1L, 1L, false, true));
            is = performGesture();
            this.x = x;
            this.y = y;
            this.m = true;
            sleep(j);
        }
        return is;
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public boolean xtouchMove(int x, int y, long j) {
        boolean is;
        synchronized (this.runTouch) {
            if (this.m) {
                if (this.x == x && this.y == y) {
                    return false;
                }
                this.bsp.add(createGestureDescription(this.x, this.y, x, y, 1L, j, true, true));
                is = performGesture();
                this.x = x;
                this.y = y;
                return is;
            }
            return false;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public boolean xtouchUp(int x, int y, long j) {
        synchronized (this.runTouch) {
            GestureDescription gestureDescription = createGestureDescription(this.x, this.y, x, y, 1L, 1L, true, false);
            if (gestureDescription == null) {
                return false;
            }
            this.bsp.add(gestureDescription);
            boolean is = performGesture();
            this.m = false;
            sleep(j);
            return is;
        }
    }

    private void sleep(long j) {
        if (j > 0) {
            try {
                Thread.sleep(j);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public  GestureDescription createGestureDescription(int startX, int startY, int endX, int endY, long duration, long startTime, boolean continueStroke, boolean isUp) {
        try {
            Path path = new Path();
            path.moveTo(startX, startY);
            if (startX != endX || startY != endY) {
                path.lineTo(endX, endY);
            }
            GestureDescription.StrokeDescription strokeDescription = !continueStroke ?
                    new GestureDescription.StrokeDescription(path, startTime, duration, isUp) :
                    mStrokeDescription.continueStroke(path, startTime, duration, isUp);
            GestureDescription.Builder builder = new GestureDescription.Builder();
            builder.addStroke(strokeDescription);
            GestureDescription gestureDescription = builder.build();
            mStrokeDescription = strokeDescription;
            return gestureDescription;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public boolean performGesture() {
        GestureDescription gestureDescription = this.bsp.get(0);

        if (gestureDescription != null) {
            return OpenApi.getAcc().dispatchGesture(gestureDescription, new AccessibilityService.GestureResultCallback() {
                @Override
                public void onCancelled(GestureDescription gestureDescription1) {
                    try {
                        multi.this.bsp.remove(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    super.onCancelled(gestureDescription1);
                }

                @Override
                public void onCompleted(GestureDescription gestureDescription1) {
                    synchronized (multi.this.runTouch) {
                        if (multi.this.bsp.isEmpty()) {
                            return;
                        }
                        multi.this.bsp.remove(0);
                        if (multi.this.bsp.isEmpty()) {
                            return;
                        }
                        multi.this.performGesture();
                        super.onCompleted(gestureDescription1);
                    }
                }
            }, null);
        }
        multi.this.bsp.clear();
        return false;
    }

}
